import 'package:equatable/equatable.dart';
import 'package:learning_content_admin_pannel.home.words/single_word_base_page/domain/entities/word_base_input.dart';

part 'result.dart';

abstract class SwapUseCaseInputs {
  UseCaseInputSwapResult call({
    required String firstUseCaseInputId,
    required String secondUseCaseInputId,
    required WordBaseInput wordBaseInput,
  });
}

class SwapUseCaseInputsImpl implements SwapUseCaseInputs {
  const SwapUseCaseInputsImpl();

  @override
  UseCaseInputSwapResult call({
    required String firstUseCaseInputId,
    required String secondUseCaseInputId,
    required WordBaseInput wordBaseInput,
  }) {
    final firstIndex = wordBaseInput.useCaseInputs.indexWhere((useCaseInput) {
      return useCaseInput.id == firstUseCaseInputId;
    });

    final secondIndex = wordBaseInput.useCaseInputs.indexWhere((useCaseInput) {
      return useCaseInput.id == secondUseCaseInputId;
    });

    final updatedUseCaseInputs = wordBaseInput.useCaseInputs.rebuild((useCaseInputs) {
      final tmp = useCaseInputs[firstIndex];
      useCaseInputs[firstIndex] = useCaseInputs[secondIndex];
      useCaseInputs[secondIndex] = tmp;
    });

    final updatedWordBaseInput = wordBaseInput.copyWith(
      useCaseInputs: () => updatedUseCaseInputs,
    );

    final result = UseCaseInputSwapResult(
      wordBaseInput: updatedWordBaseInput,
    );

    return result;
  }
}
